package com.android.mios.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.mios.R;
import com.android.mios.model.Category;

public class DeviceTreeListPopAdapter extends BaseExpandableListAdapter {
	private LayoutInflater mInflater;
	private ArrayList<HashMap<String, Object>> groupList;
	private ArrayList<ArrayList<HashMap<String, Object>>> childList;

	public DeviceTreeListPopAdapter(Context context,
			ArrayList<HashMap<String, Object>> groupList,
			ArrayList<ArrayList<HashMap<String, Object>>> childList) {
		mInflater = LayoutInflater.from(context);
		this.groupList = groupList;
		this.childList = childList;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groupList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return childList.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groupList.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null)
			convertView = mInflater.inflate(R.layout.grouplayout, null);

		ImageView iv_device = (ImageView) convertView
				.findViewById(R.id.iv_device);
		TextView txt_name = (TextView) convertView.findViewById(R.id.groupName);
		TextView txt_count = (TextView) convertView
				.findViewById(R.id.groupCount);
		if (groupList.get(groupPosition).containsKey("category")) {
			int category = (Integer) groupList.get(groupPosition).get(
					"category");

			if (category == Category.DIMMABLE_LIGHT
					|| category == Category.SWITCH)
				iv_device.setBackgroundResource(R.drawable.light_icon);
			else if (category == Category.WINDOW_COVERING)
				iv_device.setBackgroundResource(R.drawable.curtain_icon);
			else if (category == Category.DOOR_LOCK)
				iv_device.setBackgroundResource(R.drawable.lock_icon);
			else if (category == Category.CAMERA)
				iv_device.setBackgroundResource(R.drawable.camera_icon);
			else if (category == Category.CLIMATE)
				iv_device.setBackgroundResource(R.drawable.climate_icon);
			else if (category == Category.SECURITY_SENSOR)
				iv_device.setBackgroundResource(R.drawable.sensor_icon);
		} else {
			iv_device.setBackgroundResource(R.drawable.scene_icon);
		}
		String name = (String) groupList.get(groupPosition).get("name");
		txt_name.setText(name);
		String count = (String) groupList.get(groupPosition).get("count");
		txt_count.setText("[" + count + "]");

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null)
			convertView = mInflater.inflate(R.layout.childlayout, null);

		TextView txt_name = (TextView) convertView.findViewById(R.id.childname);
		TextView txt_id = (TextView) convertView.findViewById(R.id.childID);

		String name = (String) childList.get(groupPosition).get(childPosition)
				.get("name");
		txt_name.setText(name);

		String id = String.valueOf(childList.get(groupPosition)
				.get(childPosition).get("id"));

		//String serialNumber = String.valueOf(childList.get(groupPosition)
		//		.get(childPosition).get("serialNumber"));

		txt_id.setText(id);


		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
}
